﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Group3.MediaTool.Interface;

namespace Group3.MediaTool.Data.IO
{
    public static class StaticConfig
    {

        #region indentifier

        private static String _defaultIdentifierData = "data";
        private static String _identifierData;
        public static String identifierData
        {
            get
            {
                if (String.IsNullOrEmpty(StaticConfig._identifierData))
                {
                    return _defaultIdentifierData;
                }
                else
                {
                    return StaticConfig._identifierData;
                }
            }
            set
            {
                StaticConfig._identifierData = value;
            }
        }


        private static String _defaultIdentifierReplace = "rep_";
        private static String _identifierReplace;
        public static String identifierReplace
        {
            get
            {
                if (String.IsNullOrEmpty(StaticConfig._identifierReplace))
                {
                    return _defaultIdentifierReplace;
                }
                else
                {
                    return StaticConfig._identifierReplace;
                }
            }
            set
            {
                StaticConfig._identifierReplace = value;
            }
        }

        #endregion

        #region DB Types

        private static e_DataStoreProvider _defaultDatabase = e_DataStoreProvider.MSSQLServer;
        private static e_DataStoreProvider _database;
        public static e_DataStoreProvider Database
        {
            get
            {
                return (int)_database <= 0 ? _defaultDatabase : _database;
            }
            set { _database = value; }
        }


        #endregion


        #region pathDefinition

        private static String defaultPathDataDefinition = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataDefinition\\");
        //private static String defaultPathDataDefinition = Path.Combine("C:\\Users\\Armari\\VS_Projekte\\g3MediaTool", "DataDefinition\\");
        private static String _pathDataDefinition = "";
        public static String pathDataDefinition
        {
            get
            {
                if (String.IsNullOrEmpty(StaticConfig._pathDataDefinition))
                {
                    return defaultPathDataDefinition;
                }
                else
                {
                    return StaticConfig._pathDataDefinition;
                }
            }
            set
            {
                if (value != StaticConfig._pathDataDefinition)
                {
                    lock (thisLock)
                    {
                        StaticConfig._pathDataDefinition = value;
                    }
                }
            }
        }

        private static String defaultPathDataDefinitionFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataDefinition\\DataDefinition.xml");
        //private static String defaultPathDataDefinitionFile = Path.Combine("C:\\Users\\Armari\\VS_Projekte\\g3MediaTool", "DataDefinition\\DataDefinition.xml");
        private static String _pathDataDefinitionFile = "";
        public static String pathDataDefinitionFile
        {
            get
            {
                if (String.IsNullOrEmpty(StaticConfig._pathDataDefinitionFile))
                {
                    return defaultPathDataDefinitionFile;
                }
                else
                {
                    return StaticConfig._pathDataDefinitionFile;
                }
            }
            set
            {
                if (value != StaticConfig._pathDataDefinitionFile)
                {
                    lock (thisLock)
                    {
                        StaticConfig._pathDataDefinitionFile = value;
                        _classConfig = XDocument.Load(pathDataDefinitionFile);
                    }
                }
            }
        }

        #endregion

        #region Database Connections

        private static String defaultMasterDatabaseConnection = "Server=localhost;Integrated security=SSPI;database=master";
        private static String _masterDatabaseConnection = "";
        public static String masterDatabaseConnection
        {
            get
            {
                if (String.IsNullOrEmpty(_masterDatabaseConnection))
                {
                    return defaultMasterDatabaseConnection;
                }
                else
                {
                    return _masterDatabaseConnection;
                }
            }
            set
            {
                _masterDatabaseConnection = value;

            }

        }

        private static String defaultDatabaseConnection = "Server=localhost;Integrated security=SSPI;database=Group3_BookTool";
        private static String _databaseConnection = "";
        public static String databaseConnection
        {
            get
            {
                if (String.IsNullOrEmpty(_databaseConnection))
                {
                    return defaultDatabaseConnection;
                }
                else
                {
                    return _databaseConnection;
                }
            }
            set
            {
                _databaseConnection = value;
            }
        }

        #endregion

        #region Config-File

        private static Object thisLock = new Object();

        static XDocument _classConfig;
        public static XDocument classConfig
        {
            get
            {
                if (_classConfig == null)
                {
                    try
                    {
                        lock (thisLock)
                        {
                            _classConfig = XDocument.Load(pathDataDefinitionFile);
                        }
                    }
                    catch (Exception e)
                    {
                        DataDefinitionIO ddio = new DataDefinitionIO();
                        ddio.writeLogFile("XML unable to load:   Stack trace: " + e.ToString(), "Error");
                        return null;
                    }

                }
                return _classConfig;
            }
        }



        public static List<String> fixedAttributesInXML = new List<string>()
        {
                @"<attribute><!-- ID --><change></change><name>Id</name><type>bigint</type><key>1</key><index>0</index><nullable></nullable></attribute>",
                @"<attribute><!-- created --><change></change><name>created</name><type>datetime</type><key>0</key><index>0</index><nullable></nullable></attribute>",
                @"<attribute><!-- modified --><change></change><name>modified</name><type>datetime</type><key>0</key><index>0</index><nullable></nullable></attribute>"
        }
        ;

        #endregion

        public static String classAssembly = "Group3.MediaTool.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
        //public static String classAssembly = "Group3.MediaTool.TestClassenBibliothek, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

        public static String classAssemblyDll = "C:\\Users\\Armari\\VS_Projekte\\g3MediaTool\\Source\\Group3.MediaTool\\Group3.MediaTool.TestClassenBibliothek\\bin\\debug\\Group3.MediaTool.TestClassenBibliothek.dll";
        //public static String classAssemblyDll = Path.GetFullPath(@"C:\Users\Armari\VS_Projekte\g3MediaTool\Source\Group3.MediaTool\Group3.MediaTool.ConsoleSample\bin\Debug\Classes\Cache\Group3.MediaTool.ConsoleSample.vshost.exe\assembly\dl3\bef8a455\613e264f_fd70cf01\E_BOOK.dll").ToString();

        public static void addClassToConfig(XDocument newClass)
        {
            try
            {
                classConfig.Root.Add(newClass.Root);
                lock (thisLock)
                {
                    classConfig.Save(pathDataDefinitionFile);
                }
            }
            catch (Exception)
            {

            }
        }



    }



}
